//
//  CJShadowListTableViewCell.m
//  KaiJing
//
//  Created by lanou on 16/1/16.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "CJShadowListTableViewCell.h"
#import <UIButton+WebCache.h>

@interface CJShadowListTableViewCell ()

@property (nonatomic, strong) UILabel *time;

@property (nonatomic, strong) UILabel *directors;

@property (nonatomic, strong) UILabel *casts;

@end


@implementation CJShadowListTableViewCell




- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     
        _imageButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _imageButton.frame = CGRectMake(15, 10, (kScreenWidth - 15) / 2, (kScreenWidth - 15) / 2 * 3 / 2);
        _imageButton.backgroundColor = kRandomClolor;
        [self.contentView addSubview:_imageButton];
        
        //片名
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(_imageButton.right + 15, _imageButton.top, kScreenWidth - _imageButton.right - 30, 17)];
        name.text = @"片名";
        name.textColor = [UIColor lightGrayColor];
        name.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:name];
        
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(name.left, name.bottom + 5, name.width, name.height)];
        
        _nameLabel.textColor = [UIColor grayColor];
        _nameLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_nameLabel];
        
        
        
        
        //上映时间
        _time = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.left, _nameLabel.bottom + 5, _nameLabel.width, _nameLabel.height)];
        _time.text = @"上映时间";
        _time.textColor = [UIColor lightGrayColor];
        _time.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_time];
        
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_time.left, _time.bottom + 5, _time.width, _time.height)];
        _timeLabel.numberOfLines = -1;
        _timeLabel.textColor = [UIColor grayColor];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_timeLabel];
        
        
        
        //导演
        
        _directors = [[UILabel alloc] initWithFrame:CGRectMake(_timeLabel.left, _timeLabel.bottom + 5, _timeLabel.width, _timeLabel.height)];
        _directors.text = @"导演";
        _directors.textColor = [UIColor lightGrayColor];
        _directors.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_directors];
        
        
        _directorsLabel = [[UILabel alloc] initWithFrame:CGRectMake(_directors.left, _directors.bottom + 5, _directors.width, _directors.height)];
        
        _directorsLabel.textColor = [UIColor grayColor];
        _directorsLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_directorsLabel];
        
        
        
        
        //主演
        
        _casts = [[UILabel alloc] initWithFrame:CGRectMake(_directorsLabel.left, _directorsLabel.bottom + 10, _directorsLabel.width, _directorsLabel.height)];
        _casts.text = @"主演";
        _casts.textColor = [UIColor lightGrayColor];
        _casts.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_casts];
        
        
        _castsLabel = [[UILabel alloc] initWithFrame:CGRectMake(_casts.left, _casts.bottom + 5, _casts.width, _casts.height)];
        _castsLabel.numberOfLines = -1;
        _castsLabel.textColor = [UIColor grayColor];
        _castsLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_castsLabel];
        
        
        
        
        //简介
        _messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageButton.left, _imageButton.bottom + 15, kScreenWidth - 30, 20)];
        _messageLabel.numberOfLines = -1;
        _messageLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_messageLabel];
        
        
        
    
        //点赞等
        _bottomView = [[CJCellBottomView alloc] initWithFrame:CGRectMake(0, _messageLabel.bottom + 15, kScreenWidth, 25)];
        
        
        //点赞
        [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_norml"] forState:(UIControlStateNormal)];
        [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_select"] forState:(UIControlStateSelected)];
        _bottomView.praiseLabel.text = @"赞";
        //评论
        [_bottomView.commentButton setImage:[UIImage imageNamed:@"comment"] forState:(UIControlStateNormal)];
        _bottomView.commentLabel.text = @"评论";
        
        //更多
        [_bottomView.moreButton setImage:[UIImage imageNamed:@"more"] forState:(UIControlStateNormal)];

        
        [self.contentView addSubview:_bottomView];
        
        
    }
    return self;
}




- (void)setModel:(CJShadowMovieListModel *)model {
    
    _model = model;
    
    CGFloat width =  kScreenWidth - _imageButton.right - 30;
      
    NSString *urlStr = [NSString stringWithFormat:@"http://morguo.com/%@",_model.itemdata.image];
    
    [_imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:urlStr] forState:(UIControlStateNormal)];
    
    _nameLabel.text = _model.itemdata.subject;
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    
    
    
    //NSString *data = [NSString stringWithFormat:@"%@",]
    if (_model.itemdata.moviedata.pubdate.length != 0) {
        //上映时间
        CGRect timeframe = [_model.itemdata.moviedata.pubdate boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
        
        _timeLabel.text = _model.itemdata.moviedata.pubdate;
        _timeLabel.frame = CGRectMake(_time.left, _time.bottom + 5, _time.width, timeframe.size.height);
        
    }

    
    //导演    _nameLabel.bottom + 32 + timeframe.size.height
    _directors.frame = CGRectMake(_timeLabel.left, _timeLabel.bottom + 5, _timeLabel.width, _nameLabel.height);
    
    
    _directorsLabel.text = _model.itemdata.moviedata.directors;
    CGRect directorsFrame = [_model.itemdata.moviedata.directors boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    _directorsLabel.frame = CGRectMake(_directors.left, _directors.bottom + 5, _directors.width, directorsFrame.size.height);

    
    
    //主演
    _casts.frame = CGRectMake(_directorsLabel.left, _directorsLabel.bottom + 5, _directorsLabel.width, _directorsLabel.height);
    
    _castsLabel.text = _model.itemdata.moviedata.casts;
    CGRect castsLabelFrame = [_model.itemdata.moviedata.casts boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    _castsLabel.frame = CGRectMake(_casts.left, _casts.bottom + 5, _casts.width, castsLabelFrame.size.height);

    
    
    _messageLabel.text = _model.itemdata.intro;
    CGFloat height = [CJShadowListTableViewCell cellOfMessageLabelHeight:_model];
    //简介
    _messageLabel.frame = CGRectMake(_imageButton.left, _imageButton.bottom + 15, kScreenWidth - 30, height);
    
    
    //点赞等
    _bottomView.frame = CGRectMake(0, _messageLabel.bottom + 15, kScreenWidth, 25);
    if (model.itemdata.isliked) {
        _bottomView.praiseButton.selected = YES;
        
    } else {
        _bottomView.praiseButton.selected = NO;
    }


    
    
}

+(CGFloat)cellOfMessageLabelHeight:(CJShadowMovieListModel *)model {
    
     NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
     CGRect Frame = [model.itemdata.intro boundingRectWithSize:CGSizeMake(kScreenWidth - 30, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    
    return Frame.size.height;
 
}














- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
